package com.fanqwz.kotlinmvvm.di.module

import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import com.fanqwz.kotlinmvvm.presenter.viewmodel.login.LoginViewModel
import dagger.Module
import dagger.Provides
import javax.inject.Singleton

/**
 * Created by fanqwz on 2018/3/8.
 */

/**这里提供全局的viewmodel注入
 * Viewmodel的生命周期大于activity的生命周期，所以不能再activity module 中注入viewmodel，必须在这里注入*/
@Module
abstract class ViewModelProviderFactoryModule {
    @Module
    companion object {
        @JvmStatic
        @Provides
        @Singleton
        fun providerFactory(loginViewModel: LoginViewModel): ViewModelProvider.Factory {
            return object : ViewModelProvider.Factory {
                override fun <T : ViewModel?> create(modelClass: Class<T>): T {
                    if (modelClass.isAssignableFrom(LoginViewModel::class.java))
                        return loginViewModel as T
                    else
                        throw IllegalArgumentException("class can't be inject:$modelClass")
                }
            }
        }

    }
}